package com.link.base.tencent.corpwx.agent.controller;

import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.agent.model.Agent;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信应用管理
 *
 * @author 欧阳振强
 * @date 2020/01/02
 */
@Controller
@RequestMapping("/link/corpwx/agent")
public class AgentController extends BasicController<Agent> {

    @Resource
    private AgentService agentService;

    @Override
    public BasicService<Agent> getBasicService() throws Exception {
        return agentService;
    }

    /**
     * 重置sessionKey
     * @author guoshan
     * @date 2019/1/2
     */
    @RequestMapping(value = "/resetSessionKey")
    @ResponseBody
    public Map<String, Object> getSignature(String  agentId,
                                            String code, HttpServletResponse response) {
        LCLogger.info().withMessageKey("getSignature").withMessage("开始生成微信签名.").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            // 应用Id
            if (StringUtils.isBlank(agentId)) {
                throw new BasicServiceException("agentId不能为空！");
            }
            JSONObject sessionKey = agentService.getSessionKey(agentId, code);
            agentService.cacheSessionKey(sessionKey);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
